﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Chair : System.Web.UI.Page
{
    Tickets tk = new Tickets();
    public string Cinema(object obj)
    {
        return tk.GetCinemaNameByID(obj.ToString());
    }
    public string Status(object obj)
    {
        if (0 == int.Parse(obj.ToString()))
        {
            return "Using";
        }
        else
        {
            return "Fail";
        }

    }
    public static int m_se=0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(this.IsPostBack==false)
        {
            txtChairID.Text = tk.GetChairID();
            ddlCinemaName.DataSource = tk.LoadCinema();
            ddlCinemaName.DataTextField = "Name";
            ddlCinemaName.DataValueField = "CinemaID";
            ddlCinemaName.DataBind();
            ddlCinemaS.DataSource = tk.LoadCinema();
            ddlCinemaS.DataTextField = "Name";
            ddlCinemaS.DataValueField = "CinemaID";
            ddlCinemaS.DataBind();
            grvData.DataSource = tk.LoadChair();
            grvData.DataBind();
        }
    }
    protected void btnSummit_Click(object sender, EventArgs e)
    {
        if(tk.AddChair(tk.GetChairID(),ddlCinemaName.SelectedValue.ToString(),int.Parse(txtTier.Text.Trim()),int.Parse(txtChairNumber.Text.Trim()),int.Parse(ddlStatus.SelectedValue.ToString()),txtNotes.Text))
        {
            txtChairID.Text = tk.GetChairID();
            txtNotes.Text = null;
            ddlStatus.SelectedIndex = 0;
            grvData.DataSource = tk.LoadChair();
            grvData.DataBind();
        }

    }
    protected void grvDataCancel(object sender, GridViewCancelEditEventArgs e)
    {
        grvData.EditIndex = -1;
        grvData.DataSource = tk.LoadChair();
        grvData.DataBind();
    }
    protected void grvDataEdit(object sender, GridViewEditEventArgs e)
    {
        grvData.EditIndex = e.NewEditIndex;
        grvData.DataSource = tk.LoadChair();
        grvData.DataBind();
    }
    protected void grvDataUpload(object sender, GridViewUpdateEventArgs e)
    {
        if (tk.UpdateChair(grvData.DataKeys[e.RowIndex].Value.ToString(), int.Parse((grvData.Rows[e.RowIndex].Cells[4].Controls[0] as DropDownList).SelectedValue.ToString()),(grvData.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox).Text))
        {
            if (m_se == 0)
            {
                grvData.DataSource = tk.LoadChair();
                grvData.DataBind();
            }
            else
            {
                grvData.DataSource = tk.LoadChairByCinema(ddlCinemaS.SelectedValue.ToString());
                grvData.DataBind();
            }
        }
    }
    protected void btnSearch0_Click(object sender, EventArgs e)
    {
        if(""==txtSearch.Text)
        {
            grvData.DataSource = tk.LoadChairByCinema(ddlCinemaS.SelectedValue.ToString());
            grvData.DataBind();
            m_se = 1;
        }
        else
        {
            grvData.DataSource = tk.LoadChairByCinemaAndID(ddlCinemaS.SelectedValue.ToString(),txtSearch.Text);
            grvData.DataBind();
        }
    }
    protected void grvIndex(object sender, GridViewPageEventArgs e)
    {
        if (m_se == 0)
        {
            grvData.PageIndex = e.NewPageIndex;
            grvData.DataSource = tk.LoadChair();
            grvData.DataBind();
        }
        else
        {
            grvData.PageIndex = e.NewPageIndex;
            grvData.DataSource = tk.LoadChairByCinema(ddlCinemaS.SelectedValue.ToString());
            grvData.DataBind();
        }
    }
}